package com.billdowney.qq.robot.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public  class ShiJianUtil {

    public static Date date(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
       // System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
             df.format(new Date());
        Date date = null;
        //获得SimpleDateFormat类，我们转换为yyyy-MM-dd的时间格式

        try {
            date = df.parse( df.format(new Date()));
        } catch (ParseException e) {
         //   System.out.println(e.getMessage());
        }
        //System.out.println(date);
        return date;
    }
//

    /**
     * 获取到两个时间相差的月份,不包含enddate所在的月份
     * @param begindate
     * @param enddate
     * @throws ParseException
     */
    public static List<Integer> getMonth(String begindate, String enddate) throws ParseException {

        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = date.parse(begindate);
        Date d2 = date.parse(enddate);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        int a =(c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR))*12+c2.get(Calendar.MONTH)-c1.get(Calendar.MONTH);
        String[] str = begindate.split("-");
        String month = str[1];
        int monthnum = Integer.valueOf(month);
        List<Integer> lists = new ArrayList<Integer>();
        lists.add(monthnum);
        int t = 1;
        for(int i=1;i <a;i++){
            int num = monthnum + i;
            if(num < 13){
                lists.add(num);
            }else{
                int mnum = num-12*t;
                lists.add(mnum);
                if(mnum == 12){
                    t = t+1;
                }
            }
        }
        return lists;
    }
    /**
     * 返回指定的时间段内的有所日期
     *
     * @param startDate 开始时间,例:2016-02-25
     * @param endDate   结束时间,例:2016-03-05
     * @return
     */
    public static String[] dateSplit(String startDate, String endDate) {
        String[] ar = {};
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date newstartDate;
        try {
            newstartDate = simpleDateFormat.parse(startDate);
            Date newendDate = simpleDateFormat.parse(endDate);
            List<String> dateList = new ArrayList<String>();
            Long spi = newendDate.getTime() - newstartDate.getTime();
            Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数
            List<Date> newdateList = new ArrayList<Date>();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            newdateList.add(newstartDate);
            dateList.add(startDate);
            for (int i = 1; i <= step; i++) {
                dateList.add(formatter.format(new Date(newdateList.get(i - 1).getTime() + (24 * 60 * 60 * 1000))));
                newdateList.add(new Date(newdateList.get(i - 1).getTime() + (24 * 60 * 60 * 1000)));// 比上一天+一
            }
            String[] strings = new String[dateList.size()];
            String[] array = dateList.toArray(strings);
            return array;
        } catch (ParseException e) {
            e.printStackTrace();
            return ar;
        }
    }


        //添加月份   时间  —— 几 yue
    public static String monthAddFrist(String date ,int yue) {

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Calendar ct = Calendar.getInstance();
            ct.setTime(df.parse(date));
            ct.add(Calendar.MONTH, +yue);
            return df.format(ct.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "";

    }

        public static  boolean shijianduibi(String beginTim, String endTime) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        boolean before =false;
        try {
            Date date1 = format.parse(beginTim);
            Date date2 = format.parse(endTime);

             before = date1.before(date2);

            System.out.println(before);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return before;
}

    /**
     * 返回 yyyy-MM-dd 字符串类型时间
     *
     * @param ss 传入时间 Date类型
     * @return
     */

    public static String shijian(Date ss){
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        //System.out.println(simpleDateFormat.format(ss));
        return  simpleDateFormat.format(ss);
    }
    /**
     * 返回 yyyy-MM-dd HH:mm:ss字符串类型时间
     *
     * @param ss 传入时间 Date类型
     * @return
     */

    public static String shijianshifenmiao(Date ss){
        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        //System.out.println(simpleDateFormat.format(ss));
        return  simpleDateFormat.format(ss);
    }
    /**
     * 返回 yyyy-MM-dd HH:mm字符串类型时间
     *
     * @param ss 传入时间 Date类型
     * @return
     */

    public static String shijianshifen(Date ss){
        String pattern = "yyyy-MM-dd HH:mm";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        //System.out.println(simpleDateFormat.format(ss));
        return  simpleDateFormat.format(ss);
    }
    public static String shifen(Date ss){
        String pattern = "HH:mm";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        //System.out.println(simpleDateFormat.format(ss));
        return  simpleDateFormat.format(ss);
    }
    /**
     * 获取六个月前的时间点
     *
     * */


    public static String liugeyue() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        c.add(Calendar.MONTH, -6);

        Date m = c.getTime();

        String mon = format.format(m);

        System.out.println("过去六个月：" + mon);
return mon;

    }
    /**
     * 更改时间为 年-月-日
     *
     * */


    public static String nianyueri(String ss) {
        String now=null;
    try {
        Date date = new SimpleDateFormat("yyyy-MM-dd").parse(ss);
         now = new SimpleDateFormat("yyyy年MM月dd日").format(date);
    }catch (Exception e){
        System.out.println("时间类型报错");
    }

return now;

    }

    //将时间转换为时间戳
    public static String dateToStamp(String s) throws Exception {
        String res;      //设置时间格式，将该时间格式的时间转换为时间戳
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long time = date.getTime();
        res = String.valueOf(time);
        return res;
    }
    //将时间戳转换为时间
    public static String stampToTime(String s) throws Exception{
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);//将时间戳转换为时间
        Date date = new Date(lt);//将时间调整为yyyy-MM-dd HH:mm:ss时间样式
        res = simpleDateFormat.format(date);
        return res;
    }





/**
 * 计算两个时间差
 * */
    public static String getDatePoor(Date endDate, Date nowDate) {
    if(endDate!=null&&nowDate!=null) {


        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        String Date= day + "天" + hour + "小时" + min + "分钟";

        if(Date.contains("-")){
           Date= Date.replace("-", "");
           Date="-"+Date;
        }
        return Date;

    }
    return "";
    }

/**
 * 时间+小时.分钟
 *传参数 7.5
 * 7   ，   5
 *
 * */
    public static String addDateMinut(String day, int hour,Double fenzhong){
        Double ss1=0.0;
                ss1=fenzhong;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
        System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
         Double ss  = ss1/10.0;
        int v = (int) (60 * ss);
        cal.add(Calendar.MINUTE,v);// 24小时制
        date = cal.getTime();
        System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return format.format(date);

    }
    /**
     * 获取两个月份之间的所有月份(含跨年)
     *
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public static List<String> getBetweenMonths(Date start, Date end) {





        List<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM");

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        // 加了一个月
        tempStart.add(Calendar.MONTH, 1);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        result.add(sdf.format(start));
        while (tempStart.before(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.MONTH, 1);
        }
        return result;

    }


    /**
     * 判断时间是否大于当前时间
     * */

   public static boolean shijianduibi(String str2) {
        String str1 = ShiJianUtil.shijian(new Date());

        int res = str1.compareTo(str2);
        if (res > 0)
        return false;
        else if (res == 0)
        return true;
        else
        return true;


    }



}
